<!--
    Author : Christian Hadiwinoto 13507081
-->

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include 'page/abstractPage.php';

//1. Nama kelas samakan dengan nama file - WAJIB extend AbstractPage
class ServerMengelolaJenisKegiatan extends AbstractPage {

    private $data;
    private $action=''; //keterangan aksi dari button submit submit (isi sendiri dengan string)
    private $message='';//keterangan pesan berhasil atau sukses

    public function ServerMengelolaJenisKegiatan()
    {
        parent::__construct();
        $this->load->model('JenisKegiatan');

        // Session Pengguna dan Redirect jika tidak ada privilege
        $sess_user = $this->session->userdata('pengguna');
        if (!array_intersect($sess_user['role'], array("TU"))) {
            redirect('/');
        }
    }
    
    public function indexJenisKegiatan()
    {
        //nama action
        $this->action='index';

        //proses
        $tmpl = array (
                    'table_open'          => '<table border="1" cellpadding="4" cellspacing="0" class="table1">',

                    'heading_row_start'   => '<tr>',
                    'heading_row_end'     => '</tr>',
                    'heading_cell_start'  => '<th>',
                    'heading_cell_end'    => '</th>',

                    'row_start'           => '<tr>',
                    'row_end'             => '</tr>',
                    'cell_start'          => '<td>',
                    'cell_end'            => '</td>',

                    'row_alt_start'       => '<tr>',
                    'row_alt_end'         => '</tr>',
                    'cell_alt_start'      => '<td>',
                    'cell_alt_end'        => '</td>',

                    'table_close'         => '</table>'
              );
        $this->table->set_template($tmpl);
        $this->table->set_heading(
                'ID',
                'Jenis Kegiatan',
                'Edit', 'Hapus'
                );
        $hasil = $this->JenisKegiatan->getAllJenisKegiatan();
        foreach ($hasil as $row) {
            $linkEdit = "ServerMengelolaJenisKegiatan/formEditJenisKegiatan/".
                $row->typeID;
            $linkHapus = "ServerMengelolaJenisKegiatan/deleteJenisKegiatan/".
                $row->typeID;
            $this->table->add_row($row->typeID, $row->typeName,
                    '<a onClick="window.location=\''. site_url($linkEdit) .'\'">Edit</a>',
                    '<a onClick="confirmDelete(\''.$row->typeName.'\',\''.
                    site_url($linkHapus).'\');">Hapus</a>'
                    );
        }
        $this->tabelJenisKegiatan = $this->table->generate();

        //load konten
        $this->index();
    }

    function addJenisKegiatan() {
        //nama action
        $this->action = 'add';

        //proses: operasi basis data
        $typeID = $this->input->post('typeId');
        $typeName = $this->input->post('typeName');
        
        $this->JenisKegiatan->setTypeID($typeID);
        $this->JenisKegiatan->setTypeName($typeName);
        $this->JenisKegiatan->insertJenisKegiatan();
        
        //load konten
        redirect('ServerMengelolaJenisKegiatan/indexJenisKegiatan');
    }

    function editJenisKegiatan() {
        //nama action
        $this->action = 'edit';

        //proses: operasi basis data
        $typeId = $this->input->post('typeId');
        $typeName = $this->input->post('typeName');

        $this->JenisKegiatan->setTypeID($typeId);
        $jenisKegiatan = array('typeName' => $typeName);
        $this->JenisKegiatan->changeJenisKegiatan($jenisKegiatan);

        //load konten
        redirect('ServerMengelolaJenisKegiatan/indexJenisKegiatan');
    }

    function deleteJenisKegiatan($typeId) {
        //nama action
        $this->action = 'delete';

        //proses: operasi basis data
        $this->JenisKegiatan->setTypeID($typeId);
        $this->JenisKegiatan->deleteJenisKegiatan();

        //load konten
        redirect('ServerMengelolaJenisKegiatan/indexJenisKegiatan');
    }

    function formAddJenisKegiatan() {
        //nama action
        $this->action='formAdd';

        //proses
        $tmpl = array (
                    'table_open'          => '<table border="0" cellpadding="1" cellspacing="0">',

                    'heading_row_start'   => '<tr>',
                    'heading_row_end'     => '</tr>',
                    'heading_cell_start'  => '<th>',
                    'heading_cell_end'    => '</th>',

                    'row_start'           => '<tr>',
                    'row_end'             => '</tr>',
                    'cell_start'          => '<td>',
                    'cell_end'            => '</td>',

                    'row_alt_start'       => '<tr>',
                    'row_alt_end'         => '</tr>',
                    'cell_alt_start'      => '<td>',
                    'cell_alt_end'        => '</td>',

                    'table_close'         => '</table>'
              );
        $this->table->set_template($tmpl);
        $this->load->helper('form');
        $fattr = array('name' => 'formAddJenisKegiatan',
            'id' => 'formAddJenisKegiatan'
            );
        $form = form_open('ServerMengelolaJenisKegiatan/addJenisKegiatan',$fattr);
        $field = array(
              'name'        => 'typeName',
              'id'          => 'typeName',
              'maxlength'   => '10',
              'size'        => '8'
            );
        $this->table->add_row(form_label('Jenis Kegiatan','typeName'),
                form_input($field));
        $js = 'onclick="window.location = \''.
            site_url('ServerMengelolaJenisKegiatan/indexJenisKegiatan') .'\';"';
        $tabdata = array(
            'data' => form_submit('submitAddJenisKegiatan',
                    'Tambah','id="submitAddJenisKegiatan" class="f-submit" ')."&nbsp".
                form_button('cancelAddJenisKegiatan','Batal',$js.' class="f-submit"'),
            'colspan' => '2'
            );
        $this->table->add_row($tabdata);
        $form .= "\n".$this->table->generate();
        $form .= "\n".form_close();
        $this->formTambah = $form;

        // load konten
        $this->index();
    }

    function formEditJenisKegiatan($typeId) {
        //nama action
        $this->action='formEdit';

        //proses
        $tmpl = array (
                    'table_open'          => '<table border="0" cellpadding="1" cellspacing="0">',

                    'heading_row_start'   => '<tr>',
                    'heading_row_end'     => '</tr>',
                    'heading_cell_start'  => '<th>',
                    'heading_cell_end'    => '</th>',

                    'row_start'           => '<tr>',
                    'row_end'             => '</tr>',
                    'cell_start'          => '<td>',
                    'cell_end'            => '</td>',

                    'row_alt_start'       => '<tr>',
                    'row_alt_end'         => '</tr>',
                    'cell_alt_start'      => '<td>',
                    'cell_alt_end'        => '</td>',

                    'table_close'         => '</table>'
              );
        $this->table->set_template($tmpl);
        $this->load->helper('form');
        $fattr = array('name' => 'formEditJenisKegiatan',
            'id' => 'formEditJenisKegiatan'
            );
        $jenisKegiatan = $this->JenisKegiatan->getJenisKegiatan($typeId);
        $identifier = array('typeId' => $jenisKegiatan->typeID);
        $form = form_open('ServerMengelolaJenisKegiatan/editJenisKegiatan',$fattr,$identifier);
        $this->table->add_row('ID',$jenisKegiatan->typeID);
        $field = array(
              'name'        => 'typeName',
              'id'          => 'typeName',
              'maxlength'   => '10',
              'size'        => '8',
              'value'       => $jenisKegiatan->typeName
            );
        $this->table->add_row(form_label('Jenis Kegiatan','typeName'),
                form_input($field));
        $js = 'onclick="window.location = \''.
            site_url('ServerMengelolaJenisKegiatan/indexJenisKegiatan') .'\';"';
        $tabdata = array(
            'data' => form_submit('submitEditJenisKegiatan',
                    'Simpan','id="submitEditJenisKegiatan" class="f-submit"')."&nbsp".
                form_button('cancelEditJenisKegiatan','Batal',$js.' class="f-submit"'),
            'colspan' => '2'
            );
        $this->table->add_row($tabdata);
        $form .= "\n".$this->table->generate();
        $form .= "\n".form_close();
        $this->formEdit = $form;

        // load konten
        $this->index();
    }

    function _buildContent()
    {
        switch($this->action) {
            case('index'):
                /*data yang dikirimkan*/
                $data = array(
                    'tabelJenisKegiatan' => $this->tabelJenisKegiatan,
                    'linkTambahJenisKegiatan' => anchor('ServerMengelolaJenisKegiatan/formAddJenisKegiatan',
                            'Tambah Jenis Kegiatan')
                    );
                $this->load->view("AdministrasiJenisKegiatan/IndexAdministrasiJenisKegiatan",$data);
                break;
            case('formAdd'):
                /*data yang dikirimkan*/
                $data = array('formTambah' => $this->formTambah);
                $this->load->view("AdministrasiJenisKegiatan/FormTambahJenisKegiatan",$data);
                break;
            case('formEdit'):
                /*data yang dikirimkan*/
                $data = array('formEdit' => $this->formEdit);
                $this->load->view("AdministrasiJenisKegiatan/FormEditJenisKegiatan",$data);
                break;
        }
    }
}
